package gamecode;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

public class Entity {

	protected final double maxShipMoveSpeed = 0.4;
	private Image entityImage = null;
	private Color entityColor = null;
	private DPoint position = new DPoint();
	private double direction = 0;
	private Vector2D velocity = new Vector2D();
	private Vector2D acceleration = new Vector2D();
	protected boolean isPlayerShip = false;
	
	public void setImage(Image simage) {
		entityImage = simage;
		entityImage.setCenterOfRotation(entityImage.getWidth()/2, entityImage.getHeight()/2);
	}
	
	public Image getImage() {
		return entityImage;
	}
	
	public void setColor(Color scolor) {
		entityColor = scolor;
	}
	
	public Color getColor() {
		return entityColor;
	}
	
	public DPoint getPosition() {
		return position;
	}
	
	public void setPosition(double x, double y) {
		position.setLocation(x, y);
	}
	
	public void setPosition(DPoint pos) {
		position.setLocation(pos);
	}
	
	public void setX(double x) {
		position.setLocation(x, position.getY());
	}
	
	public void setY(double y) {
		position.setLocation(position.getX(), y);
	}
	
	public void move(DPoint p) {
		position.translate(p.getX(), p.getY());
	}
	
	public void move(double x, double y) {
		position.translate(x, y);
	}
	
	public double getDirection() {
		return direction;
	}
	
	public void setDirection(double d) {
		direction = Vector2D.wrapDirection(d);
		entityImage.setRotation((float)direction);
	}
	
	public void rotate(double d) {
		direction = Vector2D.wrapDirection(direction + d);
		entityImage.setRotation((float)direction);
	}
	
	public Vector2D getVelocity() {
		return velocity;
	}
	
	public void setVelocity(Vector2D vec) {
		velocity.setVector(vec);
	}
	
	public void setVelocity(double d, double m) {
		velocity.setVector(d, m);
	}
	
	public Vector2D getAcceleration() {
		return acceleration;
	}
	
	public void setAcceleration(Vector2D vec) {
		acceleration.setVector(vec);
	}
	
	public void setAcceleration(double d, double m) {
		acceleration.setVector(d, m);
	}
	
	public void update(int delta) {
		DPoint velXY = velocity.getXY(delta);
		DPoint accelXY = acceleration.getXY(delta);
		velXY.translate(accelXY);
		velocity = DPoint.getVector2D(velXY);
		position.translate(velocity.getXY(delta).getX(), velocity.getXY(delta).getY());
		if(velocity.getMagnitude() > maxShipMoveSpeed) velocity.setMagnitude(maxShipMoveSpeed);
		if(velocity.getMagnitude() < -maxShipMoveSpeed) velocity.setMagnitude(-maxShipMoveSpeed);
	}
	
	public void drawEntity(Graphics g, Rectangle bounds, EntityPlayerShip player) {
		g.setColor(Color.white);
		g.drawOval((int)(this.getPosition().getX() + (bounds.getWidth()/2 - player.getPosition().getX())), (int)(this.getPosition().getY() + (bounds.getHeight()/2 - player.getPosition().getY())), 5, 5);
	}
	
	public void resetAll() {
		position.setLocation(0, 0);
		velocity.setVector(0, 0);
		acceleration.setVector(0, 0);
		direction = 0;
	}
}
